package com.example.hotel_lxy.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.HelpDetailsBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 帮助中心详情
 */
@Route(path = "/main/HelpCenterDetailsActivity")
public class HelpCenterDetailsActivity extends BaseActivity {
    @Autowired
    String name;
    @Autowired
    int id;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text_content)
    TextView textContent;
//    @BindView(R.id.webView)
//    LollipopFixedWebView webView;

    public static void start(String name, int id) {
        ARouter.getInstance().build(ARouterPath.getHelpCenterDetailsActivity())
                .withString("name", name)
                .withInt("id", id)
                .navigation();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_help_center_details;
    }

    @Override
    public void doWork() {
        super.doWork();
        ARouter.getInstance().inject(this);
        text1.setText("" + name);
//        WebViewUtils.init(webView);
//        webView.loadUrl("");
        showDataContent();
    }

    private void showDataContent() {
        String token = SPUtils.get(HelpCenterDetailsActivity.this, "token", "").toString();
        showLoadingDialog("请求中");
        OkHttpUtils.post()
                .url(Api.POST_VIEWSET)
                .addHeader("flag", token)
                .addParams("id", "" + id)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "帮助中心详情Exception~~~~~~~~    " + e.getMessage());
                        hideLoadingDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideLoadingDialog();
                        Log.e("TAG", "帮助中心详情Exception~~~~~~~~    " + response);
                        HelpDetailsBean helpDetailsBean = JsonUtils.parseByGson(response, HelpDetailsBean.class);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(HelpCenterDetailsActivity.this,"token","");
                            Intent intent = new Intent(HelpCenterDetailsActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        if (beanBean.getCode().equals("0")) {
                            textContent.setText(helpDetailsBean.getData().getList().getContent());
                        }

                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}